package com.xhtt.modules.app.controller.cfg;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.PageUtils;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.cfg.entity.*;
import com.xhtt.modules.cfg.service.*;
import com.xhtt.modules.sys.controller.AbstractController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 报警设置
 * </p>
 *
 * @author feipc
 * @since 2019-04-10
 */
@Api(tags = "报警设置", value = "报警设置")
@RestController
@RequestMapping("app/cfg/warn")
public class CfgWarnTaskController extends AbstractController {

    @Autowired
    private CfgWarnTaskService warnTaskService;
    @Autowired
    private CfgWarnSjService warnSjService;
    @Autowired
    private CfgWarnInsService warnInsService;
    @Autowired
    private CfgWarnShipService warnShipService;
    @Autowired
    private CfgTaskInfoService taskInfoService;
    @Autowired
    private CfgTaskSubService taskSubService;

    /**
     * 获取商检公司名列表
     */
    @GetMapping(value = "/sjCompanyList")
    @ApiOperation("获取商检公司名列表")
    @Login
    public R sjCompanyList() {
        List<String> list = warnSjService.sjCompanyList();
        return R.ok().put(Constant.DATA, list);
    }

    /**
     * 获取商检报警设置列表
     */
    @GetMapping(value = "/sjList")
    @ApiOperation("获取商检报警设置列表")
    @Login
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = warnSjService.sjList(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增商检报警设置
     */
    @PostMapping(value = "/addSj")
    @ApiOperation("新增商检报警设置")
    @Login
    public R addSj(@RequestBody CfgWarnSj cfgWarnSj) {
        warnSjService.save(cfgWarnSj);
        return R.ok();
    }

    /**
     * 删除商检报警设置
     */
    @PostMapping(value = "/deleteSj")
    @ApiOperation("删除商检报警设置")
    @Login
    public R deleteSj(@RequestBody Integer[] ids) {
        warnSjService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改商检报警设置
     */
    @PostMapping(value = "/updateSj")
    @ApiOperation("修改商检报警设置")
    @Login
    public R updateSj(@RequestBody CfgWarnSj cfgWarnSj) {
        warnSjService.updateById(cfgWarnSj);
        return R.ok();
    }

    /**
     * 获取商检报警设置
     */
    @GetMapping(value = "/getSj/{id}")
    @ApiOperation("获取商检报警设置")
    @Login
    public R getSj(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, warnSjService.getById(id));
    }

    //--------------------------------------------------------------------------

    /**
     * 获取计划任务报警设置列表
     */
    @GetMapping(value = "/taskList")
    @ApiOperation("获取计划任务报警设置列表")
    @Login
    public R taskList(@RequestParam Map<String, Object> params) {
        PageUtils page = warnTaskService.taskList(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增计划任务报警设置
     */
    @PostMapping(value = "/addTask")
    @ApiOperation("新增计划任务报警设置")
    @Login
    public R addTask(@RequestBody CfgWarnTask cfgWarnTask) {
        warnTaskService.save(cfgWarnTask);
        return R.ok();
    }

    /**
     * 删除计划任务报警设置
     */
    @PostMapping(value = "/deleteTask")
    @ApiOperation("删除计划任务报警设置")
    @Login
    public R deleteTask(@RequestBody Integer[] ids) {
        warnTaskService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改计划任务报警设置
     */
    @PostMapping(value = "/updateTask")
    @ApiOperation("修改计划任务报警设置")
    @Login
    public R updateTask(@RequestBody CfgWarnTask cfgWarnTask) {
        warnTaskService.updateById(cfgWarnTask);
        return R.ok();
    }

    /**
     * 获取计划任务报警设置
     */
    @GetMapping(value = "/getTask/{id}")
    @ApiOperation("获取计划任务报警设置")
    @Login
    public R getTask(@PathVariable("id") Integer id) {
        CfgWarnTask wt = warnTaskService.getById(id);
        CfgTaskInfo plan = taskInfoService.getOne(new LambdaQueryWrapper<CfgTaskInfo>().eq(CfgTaskInfo::getPlanType, wt.getPlan()).eq(CfgTaskInfo::getType, wt.getPlanType()).select(CfgTaskInfo::getId));
        if (null != plan) {
            wt.setPlanId(plan.getId());
            CfgTaskSub task = taskSubService.getOne(new LambdaQueryWrapper<CfgTaskSub>().eq(CfgTaskSub::getTaskId, plan.getId()).eq(CfgTaskSub::getType, wt.getTaskType()).select(CfgTaskSub::getId));
            if (null != task) {
                wt.setTaskId(task.getId());
            }
        }

        return R.ok().put(Constant.DATA, wt);
    }

//--------------------------------------------------------------------------

    /**
     * 获取巡检报警设置列表
     */
    @GetMapping(value = "/insList")
    @ApiOperation("获取巡检报警设置列表")
    @Login
    public R insList(@RequestParam Map<String, Object> params) {
        PageUtils page = warnInsService.insList(params);
        return R.ok().put(Constant.DATA, page);
    }

    /**
     * 新增巡检报警设置
     */
    @PostMapping(value = "/addIns")
    @ApiOperation("新增巡检报警设置")
    @Login
    public R addIns(@RequestBody CfgWarnIns cfgWarnIns) {
        warnInsService.save(cfgWarnIns);
        return R.ok();
    }

    /**
     * 删除巡检报警设置
     */
    @PostMapping(value = "/deleteIns")
    @ApiOperation("删除巡检报警设置")
    @Login
    public R deleteIns(@RequestBody Integer[] ids) {
        warnInsService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

    /**
     * 修改巡检报警设置
     */
    @PostMapping(value = "/updateIns")
    @ApiOperation("修改巡检报警设置")
    @Login
    public R updateIns(@RequestBody CfgWarnIns cfgWarnIns) {
        warnInsService.updateById(cfgWarnIns);
        return R.ok();
    }

    /**
     * 获取巡检报警设置
     */
    @GetMapping(value = "/getIns/{id}")
    @ApiOperation("获取巡检报警设置")
    @Login
    public R getIns(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, warnInsService.getById(id));
    }

    /**
     * 获取已经设定过的线路
     */
    @GetMapping(value = "/getExistInsList")
    public R getExistInsList() {
        List<CfgWarnIns> list = warnInsService.list();
        List<Integer> lineIds = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(list)) {
            lineIds = list.stream().map(CfgWarnIns::getLineId).distinct().collect(Collectors.toList());
        }
        return R.ok().put(Constant.DATA, lineIds);
    }


//--------------------------------------------------------------------------

    /**
     * 获取装卸船报警设置
     */
    @GetMapping(value = "/ship")
    @ApiOperation("获取装卸船报警设置")
    @Login
    public R ship() {
        CfgWarnShip ws = warnShipService.getOne(null);
        return R.ok().put(Constant.DATA, ws);
    }

//    /**
//     * 新增装卸船报警设置
//     */
//    @PostMapping(value = "/addShip")
//    @ApiOperation("新增装卸船报警设置")
//    @Login
//    public R addShip(@RequestBody CfgWarnShip cfgWarnShip) {
//        warnShipService.save(cfgWarnShip);
//        return R.ok();
//    }
//
//    /**
//     * 删除装卸船报警设置
//     */
//    @PostMapping(value = "/deleteShip")
//    @ApiOperation("删除装卸船报警设置")
//    @Login
//    public R deleteShip(@RequestBody Integer[] ids) {
//        warnShipService.removeByIds(Arrays.asList(ids));
//        return R.ok();
//    }

    /**
     * 修改装卸船报警设置
     */
    @PostMapping(value = "/updateShip")
    @ApiOperation("修改装卸船报警设置")
    @Login
    public R updateShip(@RequestBody CfgWarnShip cfgWarnShip) {
        warnShipService.updateById(cfgWarnShip);
        return R.ok();
    }

    /**
     * 获取装卸船报警设置
     */
    @GetMapping(value = "/getShip/{id}")
    @ApiOperation("获取装卸船报警设置")
    @Login
    public R getShip(@PathVariable("id") Integer id) {
        return R.ok().put(Constant.DATA, warnShipService.getById(id));
    }

}
